package com.yuanqi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuanqi.common.Result;
import com.yuanqi.dto.SalesReturnOrderDTO;
import com.yuanqi.dto.SalesReturnOrderQueryDTO;
import com.yuanqi.pojo.SalesReturnOrder;
import com.yuanqi.service.SalesReturnOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 销售退货订单Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/sales/return-order")
public class SalesReturnOrderController {

    @Resource
    private SalesReturnOrderService salesReturnOrderService;

    /**
     * 新增退货订单
     * 方法名称：addReturnOrder
     * 前端调用：POST /api/sales/return-order/add
     */
    @PostMapping("/add")
    public Result<Long> addReturnOrder(@RequestBody SalesReturnOrderDTO returnOrderDTO) {
        try {
            Long id = salesReturnOrderService.addReturnOrder(returnOrderDTO);
            return Result.success("新增退货订单成功", id);
        } catch (Exception e) {
            log.error("新增退货订单失败", e);
            return Result.error("新增退货订单失败：" + e.getMessage());
        }
    }

    /**
     * 删除退货订单
     * 方法名称：deleteReturnOrder
     * 前端调用：DELETE /api/sales/return-order/delete/{id}
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteReturnOrder(
            @PathVariable Long id) {
        try {
            boolean result = salesReturnOrderService.deleteReturnOrder(id);
            return Result.success(result ? "删除成功" : "删除失败", result);
        } catch (Exception e) {
            log.error("删除退货订单失败", e);
            return Result.error("删除退货订单失败：" + e.getMessage());
        }
    }

    /**
     * 更新退货订单
     * 方法名称：updateReturnOrder
     * 前端调用：PUT /api/sales/return-order/update
     */
    @PutMapping("/update")
    public Result<Boolean> updateReturnOrder(@RequestBody SalesReturnOrderDTO returnOrderDTO) {
        try {
            boolean result = salesReturnOrderService.updateReturnOrder(returnOrderDTO);
            return Result.success(result ? "更新成功" : "更新失败", result);
        } catch (Exception e) {
            log.error("更新退货订单失败", e);
            return Result.error("更新退货订单失败：" + e.getMessage());
        }
    }

    /**
     * 查询退货订单详情
     * 方法名称：getReturnOrderDetail
     * 前端调用：GET /api/sales/return-order/detail/{id}
     */
    @GetMapping("/detail/{id}")
    public Result<SalesReturnOrderDTO> getReturnOrderDetail(
            @PathVariable Long id) {
        try {
            SalesReturnOrderDTO detail = salesReturnOrderService.getReturnOrderDetail(id);
            return Result.success("查询成功", detail);
        } catch (Exception e) {
            log.error("查询退货订单详情失败", e);
            return Result.error("查询退货订单详情失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询退货订单
     * 方法名称：getReturnOrderPage
     * 前端调用：POST /api/sales/return-order/page
     */
    @PostMapping("/page")
    public Result<IPage<SalesReturnOrderDTO>> getReturnOrderPage(@RequestBody(required = false) SalesReturnOrderQueryDTO queryDTO) {
        try {
            if (queryDTO == null) {
                queryDTO = new com.yuanqi.dto.SalesReturnOrderQueryDTO();
            }
            IPage<SalesReturnOrderDTO> page = salesReturnOrderService.getReturnOrderPage(queryDTO);
            return Result.success("查询成功", page);
        } catch (Exception e) {
            log.error("分页查询退货订单失败", e);
            return Result.error("分页查询退货订单失败：" + e.getMessage());
        }
    }

    @GetMapping("/page")
    public Result<IPage<SalesReturnOrderDTO>> getReturnOrderPageGet(SalesReturnOrderQueryDTO queryDTO) {
        try {
            if (queryDTO == null) {
                queryDTO = new com.yuanqi.dto.SalesReturnOrderQueryDTO();
            }
            IPage<SalesReturnOrderDTO> page = salesReturnOrderService.getReturnOrderPage(queryDTO);
            return Result.success("查询成功", page);
        } catch (Exception e) {
            log.error("分页查询退货订单失败", e);
            return Result.error("分页查询退货订单失败：" + e.getMessage());
        }
    }

    /**
     * 提交审核
     * 方法名称：submitForAudit
     * 前端调用：POST /api/sales/return-order/submit/{id}
     */
    @PostMapping("/submit/{id}")
    public Result<Boolean> submitForAudit(
            @PathVariable Long id) {
        try { 
            boolean result = salesReturnOrderService.submitForAudit(id);
            return Result.success(result ? "提交审核成功" : "提交审核失败", result);
        } catch (Exception e) {
            log.error("提交审核失败", e);
            return Result.error("提交审核失败：" + e.getMessage());
        }
    }

    /**
     * 审核退货订单
     * 方法名称：auditReturnOrder
     * 前端调用：POST /api/sales/return-order/audit
     */
    @PostMapping("/audit")
    public Result<Boolean> auditReturnOrder(
            @RequestParam Long id,
            @RequestParam Integer auditResult,
            @RequestParam String auditOpinion,
            @RequestParam Long auditorId) {
        try {
            boolean result = salesReturnOrderService.auditReturnOrder(id, auditResult, auditOpinion, auditorId);
            return Result.success(result ? "审核成功" : "审核失败", result);
        } catch (Exception e) {
            log.error("审核退货订单失败", e);
            return Result.error("审核退货订单失败：" + e.getMessage());
        }
    }

    /**
     * 完成退货订单
     * 方法名称：completeReturnOrder
     * 前端调用：POST /api/sales/return-order/complete/{id}
     */
    @PostMapping("/complete/{id}")
    public Result<Boolean> completeReturnOrder(
            @PathVariable Long id) {
        try {
            boolean result = salesReturnOrderService.completeReturnOrder(id);
            return Result.success(result ? "完成成功" : "完成失败", result);
        } catch (Exception e) {
            log.error("完成退货订单失败", e);
            return Result.error("完成退货订单失败：" + e.getMessage());
        }
    }

    /**
     * 根据原销售订单ID查询退货单列表
     * 方法名称：getReturnOrdersByOrderId
     * 前端调用：GET /api/sales/return-order/by-order/{orderId}
     */
    @GetMapping("/by-order/{orderId}")
    public Result<List<SalesReturnOrder>> getReturnOrdersByOrderId(
            @PathVariable Long orderId) {
        try {
            List<SalesReturnOrder> list = salesReturnOrderService.getReturnOrdersByOrderId(orderId);
            return Result.success("查询成功", list);
        } catch (Exception e) {
            log.error("查询退货单列表失败", e);
            return Result.error("查询退货单列表失败：" + e.getMessage());
        }
    }

    /**
     * 生成退货单编号
     * 方法名称：generateReturnNo
     * 前端调用：GET /api/sales/return-order/generate-no
     */
    @GetMapping("/generate-no")
    public Result<String> generateReturnNo() {
        try {
            String returnNo = salesReturnOrderService.generateReturnNo();
            return Result.success("生成成功", returnNo);
        } catch (Exception e) {
            log.error("生成退货单编号失败", e);
            return Result.error("生成退货单编号失败：" + e.getMessage());
        }
    }

    /**
     * 检查是否可以删除
     * 方法名称：canDelete
     * 前端调用：GET /api/sales/return-order/can-delete/{id}
     */
    @GetMapping("/can-delete/{id}")
    public Result<Boolean> canDelete(
            @PathVariable Long id) {
        try {
            boolean canDelete = salesReturnOrderService.canDelete(id);
            return Result.success("检查成功", canDelete);
        } catch (Exception e) {
            log.error("检查删除权限失败", e);
            return Result.error("检查删除权限失败：" + e.getMessage());
        }
    }

    /**
     * 检查是否可以编辑
     * 方法名称：canEdit
     * 前端调用：GET /api/sales/return-order/can-edit/{id}
     */
    @GetMapping("/can-edit/{id}")
    public Result<Boolean> canEdit(
            @PathVariable Long id) {
        try {
            boolean canEdit = salesReturnOrderService.canEdit(id);
            return Result.success("检查成功", canEdit);
        } catch (Exception e) {
            log.error("检查编辑权限失败", e);
            return Result.error("检查编辑权限失败：" + e.getMessage());
        }
    }
}
